package com.yuanren.em.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanren.em.entity.Comment;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CommentMapper extends BaseMapper<Comment> {
    
    /**
     * 根据商品ID查询评论列表（包含用户信息）
     */
    @Select("SELECT c.*, u.nickname as username, u.avatar_url as avatarUrl " +
            "FROM t_comment c " +
            "LEFT JOIN sys_user u ON c.user_id = u.id " +
            "WHERE c.good_id = #{goodId} " +
            "ORDER BY c.create_time DESC")
    List<Comment> selectCommentsByGoodId(@Param("goodId") Long goodId);
    
    /**
     * 检查用户是否已经对某个订单中的商品进行了评论
     */
    @Select("SELECT COUNT(*) FROM t_comment WHERE user_id = #{userId} AND good_id = #{goodId} AND order_id = #{orderId}")
    int checkCommentExists(@Param("userId") Long userId, @Param("goodId") Long goodId, @Param("orderId") Long orderId);
    
    /**
     * 根据用户ID查询评论列表（包含商品信息）
     */
    @Select("SELECT c.*, g.name as goodName, g.imgs as goodImg " +
            "FROM t_comment c " +
            "LEFT JOIN good g ON c.good_id = g.id " +
            "WHERE c.user_id = #{userId} " +
            "ORDER BY c.create_time DESC")
    List<Comment> selectCommentsByUserId(@Param("userId") Long userId);
    
    /**
     * 分页查询所有评论（包含用户和商品信息）
     */
    @Select("<script>" +
            "SELECT c.*, u.nickname as username, u.avatar_url as avatarUrl, g.name as goodName, g.imgs as goodImg " +
            "FROM t_comment c " +
            "LEFT JOIN sys_user u ON c.user_id = u.id " +
            "LEFT JOIN good g ON c.good_id = g.id " +
            "WHERE 1=1 " +
            "<if test='goodName != null and goodName != \"\"'>" +
            "AND g.name LIKE CONCAT('%', #{goodName}, '%') " +
            "</if>" +
            "<if test='userName != null and userName != \"\"'>" +
            "AND u.nickname LIKE CONCAT('%', #{userName}, '%') " +
            "</if>" +
            "<if test='rating != null'>" +
            "AND c.rating = #{rating} " +
            "</if>" +
            "ORDER BY c.create_time DESC" +
            "</script>")
    IPage<Comment> selectCommentsPageWithInfo(IPage<Comment> page, 
                                             @Param("goodName") String goodName,
                                             @Param("userName") String userName,
                                             @Param("rating") Integer rating);
    
    /**
     * 获取商品的评论统计信息
     */
    @Select("SELECT " +
            "COUNT(*) as totalCount, " +
            "AVG(rating) as avgRating, " +
            "SUM(CASE WHEN rating = 5 THEN 1 ELSE 0 END) as fiveStarCount, " +
            "SUM(CASE WHEN rating = 4 THEN 1 ELSE 0 END) as fourStarCount, " +
            "SUM(CASE WHEN rating = 3 THEN 1 ELSE 0 END) as threeStarCount, " +
            "SUM(CASE WHEN rating = 2 THEN 1 ELSE 0 END) as twoStarCount, " +
            "SUM(CASE WHEN rating = 1 THEN 1 ELSE 0 END) as oneStarCount " +
            "FROM t_comment WHERE good_id = #{goodId}")
    CommentStatistics getCommentStatistics(@Param("goodId") Long goodId);
    
    /**
     * 评论统计信息内部类
     */
    class CommentStatistics {
        private Long totalCount;
        private Double avgRating;
        private Long fiveStarCount;
        private Long fourStarCount;
        private Long threeStarCount;
        private Long twoStarCount;
        private Long oneStarCount;
        
        // Getters and Setters
        public Long getTotalCount() {
            return totalCount;
        }
        
        public void setTotalCount(Long totalCount) {
            this.totalCount = totalCount;
        }
        
        public Double getAvgRating() {
            return avgRating;
        }
        
        public void setAvgRating(Double avgRating) {
            this.avgRating = avgRating;
        }
        
        public Long getFiveStarCount() {
            return fiveStarCount;
        }
        
        public void setFiveStarCount(Long fiveStarCount) {
            this.fiveStarCount = fiveStarCount;
        }
        
        public Long getFourStarCount() {
            return fourStarCount;
        }
        
        public void setFourStarCount(Long fourStarCount) {
            this.fourStarCount = fourStarCount;
        }
        
        public Long getThreeStarCount() {
            return threeStarCount;
        }
        
        public void setThreeStarCount(Long threeStarCount) {
            this.threeStarCount = threeStarCount;
        }
        
        public Long getTwoStarCount() {
            return twoStarCount;
        }
        
        public void setTwoStarCount(Long twoStarCount) {
            this.twoStarCount = twoStarCount;
        }
        
        public Long getOneStarCount() {
            return oneStarCount;
        }
        
        public void setOneStarCount(Long oneStarCount) {
            this.oneStarCount = oneStarCount;
        }
    }
} 